package com.sqx.modules.trendsLike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.trends.entity.Trends;
import com.sqx.modules.trends.service.ITrendsService;
import com.sqx.modules.trends.vo.TrendsVo;
import com.sqx.modules.trendsComment.entity.TrendsComment;
import com.sqx.modules.trendsComment.service.ITrendsCommentService;
import com.sqx.modules.trendsLike.dao.TrendsLikeDao;
import com.sqx.modules.trendsLike.entity.TrendsLike;
import com.sqx.modules.trendsLike.service.ITrendsLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * 动态点赞 服务实现类
 *
 * @author wuChang
 * @since 2023-09-27
 */
@Service
public class TrendsLikeServiceImpl extends ServiceImpl<TrendsLikeDao, TrendsLike> implements ITrendsLikeService {
    @Autowired
    private ITrendsService trendsService;
    @Autowired
    private ITrendsCommentService commentService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;

    @Override
    public Result saveTrendsLike(TrendsLike trendsLike) {
        Trends trends = trendsService.getById(trendsLike.getTrendsId());
        if (trendsLike.getTrendsId() != null) {
            if (trends == null) {
                return Result.error("动态不存在");
            }
        }
        List<TrendsLike> selectOne = baseMapper.selectList(new QueryWrapper<>(trendsLike)
                .eq("trends_id", trendsLike.getTrendsId())
                .eq("user_id", trendsLike.getUserId()));
        if (selectOne == null || selectOne.isEmpty()) {
            trendsLike.setIsLook(0);
            if (trendsLike.getCommentId() != null) {
                TrendsComment comment = commentService.getById(trendsLike.getCommentId());
                trendsLike.setByUserId(comment.getUserId());
            }
            UserEntity userEntity = userService.getById(trendsLike.getUserId());
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setContent("用户" + userEntity.getUserName() + "点赞了你的动态《" + trends.getTrendsContent() + "》");
            messageInfo.setTitle("点赞通知");
            messageInfo.setState(String.valueOf(6));
            messageInfo.setUserId(trends.getUserId().toString());
            messageInfo.setCreateAt(DateUtils.format(new Date()));
            messageInfo.setIsSee("0");
            messageService.saveBody(messageInfo);
            // 创建 DateTimeFormatter
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            // 将字符串转换为 LocalDateTime
            LocalDateTime dateTime = LocalDateTime.parse(formatter.format(LocalDateTime.now()), formatter);
            trendsLike.setCreateTime(dateTime);
            return Result.upStatus(baseMapper.insert(trendsLike));
        } else {
            return Result.upStatus(baseMapper.delete(new QueryWrapper<>(trendsLike)));
        }

    }

    @Override
    public IPage<TrendsLike> getLikeMyList(Long userId, Integer page, Integer limit) {
        Page<TrendsLike> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<TrendsLike> likeMyList = baseMapper.getLikeMyList(pages, userId);
        TrendsLike trendsLike = new TrendsLike();
        trendsLike.setIsLook(1);
        baseMapper.update(trendsLike, new QueryWrapper<TrendsLike>().eq("is_look", 0).eq("by_user_id", userId));

        return likeMyList;
    }
}
